function geoInf( position ) {
	//console.log(JSON.stringify(position));
	var address = position.address,
		city = address.city,
		district = address.district,
		street = address.street,
		addresses = position.addresses,
		html = '';
		if(plus.os.name=="iOS"){
			if(addresses){
				if((addresses.toString().indexOf(street.toString()) < 0)){
					html = city+district+street+addresses;
				}else{
					html = city+district+addresses;
				}
			}else{
				html = city+district+street;
			}		       
		}else{
			html = addresses;
		}	
		
	$("#search span").html(html).parent().attr({"data-district":city+district,"data-street":street});
}


// 通过定位模块获取位置信息
function getGeocode(){
	plus.geolocation.getCurrentPosition( geoInf, function ( e ) {
		console.log( "获取定位位置信息失败");
		plus.nativeUI.toast("获取定位位置信息失败");
	},{geocode:true,provider:'amap'});
}

function checkUploadWay(length,callback){
	plus.nativeUI.actionSheet({
		cancel: "取消",
		buttons: [{
			title: "从手机相册选择"
		}, {
			title: '拍照'
		}]
	},function(e){
		if (e.index == 1) { //点击从相册选择
			//console.log("相册选择");
			if(length == 1){
				if(callback){
					galleryImg(callback);
				}else{
					galleryImg();
				}	
			}else{
				galleryImgsMaximum();
			}
		} else if (e.index == 2) {
			//console.log("拍照");
			if(callback){
				getImage(callback);
			}else{
				getImage();
			}	
		}
	});
}
// 拍照
function getImage(callBack){
	//console.log('开始拍照：');
	var cmr = plus.camera.getCamera();
	cmr.captureImage(function(p){
		//console.log('成功：'+p);
		plus.nativeUI.showWaiting('',{modal:false,back:'close'});
		plus.io.resolveLocalFileSystemURL(p, function(entry){
			var dirURL = entry.toLocalURL();
			//console.log(dirURL);
			if(callBack){
				compressImage(dirURL,callBack);
			}else{
				compressImage(dirURL,showImg);
			}
			
		}, function(e){
			console.log('读取拍照文件错误：'+e.message);
		});
	}, function(e){
		console.log('失败：'+e.message);
	},{filename:'_doc/camera/',index:1});
}
//压缩图片
function compressImage(dirURL,callback){
	var time = new Date().getTime(),
		dirArr = dirURL.split('/'),
		length = dirArr.length,
		newDirArr = dirArr[length-1].split('.'),
		newDir = newDirArr[0];
	plus.zip.compressImage({
			src:dirURL,
			dst:'_doc/'+newDir+time+'.jpg',
			overwrite:true
		},
		function(event) {
			var target = event.target,
			size = event.size;
			if(callback){
				getBase64Code(target,callback);
			}
		},function(error) {
			
	});
}
function galleryImg(callback){
	plus.gallery.pick(function(path){
		plus.nativeUI.showWaiting('');
		//console.log(path);
		compressImage(path,callback); 
	},function(e){
		console.log('取消选择图片');
	},{filter:'image'});
}
function galleryImgsMaximum(){
	var length = $(".upload-img-box img").length,
		maxLength = 4 - length;
	// 从相册中选择图片
    plus.gallery.pick(function(e){
    	for(var i in e.files){
	    	compressImage(e.files[i],showImg); 
    	}
    	
    }, function(e){
    	console.log('取消选择图片');
    },{filter:'image',multiple:true,maximum:maxLength,system:false,onmaxed:function(){
		plus.nativeUI.alert('最多只能选择'+maxLength+'张图片');
    }});// 最多选择3张图片
}
function getBase64Code(path,callback){
	var bitmap =new plus.nativeObj.Bitmap();
	// 从本地加载Bitmap图片
   	bitmap.load(path,function(){
   		plus.nativeUI.closeWaiting();
       var base4 = bitmap.toBase64Data();
		callback(base4);
    },function(e){
        console.log('加载图片失败：'+JSON.stringify(e));
    }); 
}
function showImg(imgPath){
	var length = $(".upload-img-box img").length;
	if(length >= 3){
		$(".upload-img-box .img-add").hide();
	}else{
		$(".upload-img-box .img-add").show();
	}
	var html = '<div class="img-default"><img src="'+imgPath+'"/><div class="img-close"></div></div>';
	if(imgPath){
		$(".upload-img-box").prepend(html);
	}	
}